package typejump.commands;

import typejump.AnimationManager;
import typejump.Messenger;
import typejump.Settings;
import typejump.TypeJump;
import typejump.engine.GameEngine;
import typejump.engine.OnGoingThing;
import typejump.objects.Actor;
import typejump.objects.AttachableSparkles;
import typejump.objects.Sparkles;

public class Slowfall implements TextCommand, OnGoingThing {
	
	private double speed = Settings.getDouble("slowfall.maxspeed");
	private double swag = Settings.getDouble("slowfall.cost");
	
	private Sparkles sparkles;

	@Override
	public boolean matches(String command) {
		return command.equalsIgnoreCase("slowfall");
	}

	@Override
	public void act(String command) {
		GameEngine.addOnGoingThing(this);
		GameEngine.addTextEffect("SLOWFALL", "slowfall");
		
		if (sparkles != null) sparkles.stopSparkling();
		sparkles = new AttachableSparkles(GameEngine.getPlayer(), AnimationManager.SPARKLE_SLOWFALL, 100);
		sparkles.setSpeedY(150, 200);
		sparkles.setVelRuleY(Sparkles.VEL_ALLOW_POS);
		GameEngine.addEffect(sparkles);
		GameEngine.addEndEvent(this, sparkles, "stopSparkling");
	}

	@Override
	public boolean happen(TypeJump tj, double seconds) {
		Actor p = tj.getPlayer();
		if (p.isOnGround() || Messenger.hasMessage("fall"))
			return true;
		if (p.vy() >= speed) {
			if (!tj.getSwagMeter().tryExpendSwag(swag*seconds))
				return true;
			p.vy(speed);
		}
		GameEngine.blockSwagRegen();

		return false;
	}

	
}
